<?php
/**
 * CK 开发框架
 * User: Clake
 * Date: 15/6/21
 * Time: 01:46
 */

namespace CK\Core;

use CK\Util\Arr;

class Rpc extends Base{
    const RPC_HPROSE = 'HPROSE';
    const RPC_REST = 'REST';
    const RPC_SOAP = 'SOAP';

    private static $_instance = [];

    private $_conf = [
        'type'=>self::RPC_HPROSE,
        'url'=>''
    ];

    /**
     * @param string $name
     *
     * @return static
     */
    public static function inst($name='main') {
        if (!isset(self::$_instance[$name])) {
            self::$_instance[$name] = new self($name);
        }
        return self::$_instance[$name];
    }

    public function __construct($name) {
        $conf = Config::inst()->get('app.rpc');
        if (is_array($conf)) {
            $this->_conf = Arr::mrg($this->_conf,$conf);
        }
    }

    public function host() {
        switch ($this->_conf['type']) {
            case self::RPC_HPROSE:
                $server = new \HproseHttpServer();
                break;
        }
        return $server;
    }

    public function client() {
        switch ($this->_conf['type']) {
            case self::RPC_HPROSE:
                $client = new \HproseHttpClient();
                break;
        }
        return $client;
    }
}